package com.xuecheng.media.api;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author woldier
 * @version 1.0
 * @description 大文件上传接口
 * @date 2023/3/11 11:43
 **/
@Api(value = "大文件上传",tags = "大文件上传接口")
@RestController
public class BigFilesController {

    @Autowired
    MediaFileService mediaFileService;

    /**
     * @description 文件上传前的检查接口
     * @param fileMd5  文件md5值
     * @return com.xuecheng.base.model.RestResponse
     * @author: woldier
     * @date: 2023/3/11 11:54
     */
    @ApiOperation("检查文件是否存在")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkFile( @RequestParam(value = "fileMd5")  String  fileMd5){
        RestResponse<Boolean> response = mediaFileService.checkFile(fileMd5);
        return response;
    }

    /**
     * @description 检查分片是否存在
     * @param fileMd5 文件md5值
     * @param chunk  分片id
     * @return com.xuecheng.base.model.RestResponse
     * @author: woldier
     * @date: 2023/3/11 11:59
     */
    @ApiOperation("检查分片是否存在")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(@RequestParam(value = "fileMd5") String  fileMd5,@RequestParam(value = "chunk") Integer chunk){
        RestResponse<Boolean> response = mediaFileService.checkChunk(fileMd5,chunk);
        return response;
    }


    /**
     * @description 分片上传接口
     * @param file 文件
     * @param fileMd5 分片md5
     * @param chunk  分片id
     * @return com.xuecheng.base.model.RestResponse
     * @author: woldier
     * @date: 2023/3/11 12:04
     */
    @ApiOperation("上传分片")
    @PostMapping("/upload/uploadchunk")
    public RestResponse uploadChunk(@RequestParam("file") MultipartFile file,@RequestParam("fileMd5")  String fileMd5, @RequestParam("chunk")  Integer chunk){

        File tempFile = null;
        try {
            tempFile = File.createTempFile("minio",".temp");
            file.transferTo(tempFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String localFilePath = tempFile.getAbsolutePath();
        RestResponse response =  mediaFileService.uploadChunk(fileMd5,chunk,localFilePath);
        return response;
    }

    /**
     * @description 合并分片
     * @param fileName 文件名
     * @param fileMd5 文件md5值
     * @param chunkTotal  分片总数
     * @return com.xuecheng.base.model.RestResponse
     * @author: woldier
     * @date: 2023/3/11 12:18
     */
    @ApiOperation("合并分片")
    @PostMapping("/upload/mergechunks")
    public RestResponse mergeChunks(@RequestParam("fileMd5")  String fileMd5, @RequestParam("fileName") String fileName, @RequestParam("chunkTotal")  Integer chunkTotal){
        Long companyId = 1232141425L;
        //文件信息对象
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(fileName);
        uploadFileParamsDto.setTags("视频文件");
        uploadFileParamsDto.setFileType("001002");
        RestResponse restResponse = null;
        try {
            restResponse = mediaFileService.mergeChunks(companyId, fileMd5, chunkTotal, uploadFileParamsDto);
        } catch (Exception e) {
            XueChengPlusException.cast("文件上传失败");
        }
        return restResponse;
    }
}
